package com.android.progressdemo;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class ProgressView extends View {
    private static final long ANIMATION_DURATION = 1500L;
    // 画笔，用于绘制进度条的背景
    private Paint mPaintBackground;
    // 画笔，用于绘制进度条的前景
    private Paint mPaintProgress;
    // 画笔，用于绘制进度条的图标
    private Paint mPaintPicture;

    private float mPaintWidth;
    private int mBitmapSize;
    private Bitmap mBitmap;

    private float progress = 0f;

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaints();
        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
    }

    /**
     * 初始化画笔
     */
    private void initPaints() {
        mPaintBackground = new Paint();
        // 设置样式-填充
        mPaintBackground.setStyle(Paint.Style.FILL);
        // 设置圆角
        mPaintBackground.setStrokeCap(Paint.Cap.ROUND);
        // 设置颜色
        mPaintBackground.setColor(Color.GRAY);
        // 设置抗锯齿
        mPaintBackground.setAntiAlias(true);

        mPaintProgress = new Paint();
        mPaintProgress.setStyle(Paint.Style.FILL);
        mPaintProgress.setStrokeCap(Paint.Cap.ROUND);
        mPaintProgress.setColor(Color.RED);
        mPaintProgress.setAntiAlias(true);

        mPaintPicture = new Paint();
        mPaintPicture.setAntiAlias(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPaintWidth = h;
        mPaintProgress.setStrokeWidth(mPaintWidth);
        mPaintBackground.setStrokeWidth(mPaintWidth);
        mBitmapSize = h;
        mBitmap = resizeImage(mBitmap, mBitmapSize, mBitmapSize);
        invalidate();
    }

    /**
     * 重新设置bitmap的尺寸
     */
    private Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        float scaleWidth = ((float) w) / width;
        float scaleHeight = ((float) h) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // 绘制背景
        canvas.drawLine(mPaintWidth / 2.0f, getHeight() / 2.0f, getWidth() - mPaintWidth / 2.0f, getHeight() / 2.0f, mPaintBackground);

        // 计算坐标
        float x = (getWidth() - mPaintWidth) * progress / 100;
        float endX = x + mPaintWidth / 2.0f;

        // 绘制前景
        canvas.drawLine(mPaintWidth / 2.0f, getHeight() / 2.0f, endX, getHeight() / 2.0f, mPaintProgress);
        // 绘制图标
        canvas.drawBitmap(mBitmap, endX - mBitmapSize / 2.0f, getPaddingTop(), mPaintPicture);
    }

    /**
     * 设置进度
     *
     * @param progress 当前进度
     */
    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    /**
     * 设置进度，带动画
     *
     * @param progress 当前进度
     */
    public void setProgressWithAnim(float progress) {
        ValueAnimator animator = ValueAnimator.ofFloat(0, progress).setDuration(ANIMATION_DURATION);
        animator.addUpdateListener(animation -> {
            this.progress = (float) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();
    }
}
